package Customer

import (
	"fmt"
	"goroutine/Sushi"
	"time"
)

type Customer struct {
	//顾客id
	id int
	//顾客期望吃到的个数
	Wanted int
	//顾客已经吃到的个数
	Eaten int
	//吃饭速度，微秒
	Speed int
}

func GetCustomer(id int, wanted int, s int) Customer {
	c := Customer{
		id:     id,
		Wanted: wanted,
		Eaten:  0,
		Speed:  s,
	}
	return c
}

func (receiver Customer) ToString() string {
	return fmt.Sprintf("%d号顾客想吃%d个寿司，已经吃了%d个寿司", receiver.id, receiver.Wanted, receiver.Eaten)
}

func (receiver Customer) Getid() int {
	return receiver.id
}

func (receiver *Customer) TakeSushi(transporter Sushi.Transporter, exit chan bool) {
	for {
		s := <-transporter
		receiver.Eaten++
		fmt.Printf("%d号顾客取走%d号寿司\n", receiver.Getid(), s.Getid())
		if receiver.Eaten == receiver.Wanted {
			fmt.Printf("%d号顾客已经吃饱离店!!!!!!!!\n", receiver.id)
			exit <- true
			break
		}
		time.Sleep(time.Millisecond * time.Duration(receiver.Speed))
	}
}
